package com.bdqn.easybuy1.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebFilter(filterName = "EncodeFilter",
        urlPatterns = {"/*"},
        initParams = {@WebInitParam(name = "encode", value = "utf-8")})
public class EncodeFilter implements Filter {
    private String encode = null;

    public void init(FilterConfig config) throws ServletException {
        String encode = config.getInitParameter("encode");
        if (this.encode == null) {
            this.encode = encode;
        }
    }

    public void destroy() {
        encode = null;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (null == request.getCharacterEncoding()) {
            request.setCharacterEncoding(encode);
        }
        chain.doFilter(request, response);
    }
}
